package com.hsj.springai.utils;


import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.function.Consumer;

public class TaskBatchSendUtils {

    public static <T> void execute(List<T> taskList, Executor threadPool, Consumer<? super T> consumer) throws InterruptedException {
        if (taskList == null || taskList.isEmpty()) {
            return;
        }
        if (Objects.isNull(consumer)) {
            return;
        }
        CountDownLatch countDownLatch = new CountDownLatch(taskList.size());

        for (T task : taskList) {
            threadPool.execute(() -> {
                try {
                    consumer.accept(task);
                }finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
    }
    //这里是 具体 要执行的任务
    public static void runSqlTask(MyTask mytask){
        System.out.println("线程："+Thread.currentThread().getName()+ "执行"+mytask.getTaskName()+"任务,"+"任务内容"+mytask.getTaskContent());
    }

}

